package com.nobody.facepk.persist.type;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TypeUtils {
	private static final String SEPARATOR = "\\|";
	
	public static List<Long> stringToLongList(String value){
		if(value==null) return new ArrayList<Long>();
		
		String[] tokens = value.split(SEPARATOR);
		List<Long> list = new ArrayList<Long>(tokens.length);
		for(String token : tokens) {
			try {
				list.add(Long.parseLong(token));
			} catch (Exception e) {
				System.err.print(e.getMessage());
			}
		}
		return list;
	}
	
	public static <T> List<T> stringToList(Class<T> type, String value){
		if(value==null) return new ArrayList<T>();
		
		String[] tokens = value.split(SEPARATOR);
		List<T> list = new ArrayList<T>(tokens.length);
		for(String token : tokens) {
			try {
				list.add(toType(type,token));
			} catch (Exception e) {
				System.err.print(e.getMessage());
			}
		}
		return list;
	}
	
	public static <T> Set<T> stringToSet(Class<T> type, String value){
		if(value==null) return new HashSet<T>();
		
		String[] tokens = value.split(SEPARATOR);
		Set<T> resultSet = new HashSet<T>(tokens.length);
		for(String token : tokens) {
			try {
				resultSet.add(toType(type,token));
			} catch (Exception e) {
				System.err.print(e.getMessage());
			}
		}
		return resultSet;
	}
	
	private static <T> T toType(Class<T> type, String value){
		if(String.class.isAssignableFrom(type)) return (T)value;
		else if(Long.class.isAssignableFrom(type)) return (T)new Long(value);
		else if(Integer.class.isAssignableFrom(type)) return (T)new Integer(value);
		throw new IllegalArgumentException("unable to parse to type:"+type);
	}
	
	public static String listToString(List<?> list) {
		StringBuilder buffer = new StringBuilder();
		for(Object obj : list) {
			buffer.append(String.valueOf(obj)).append("|");
		}
		return buffer.toString();
	}
	
	public static String setToString(Collection<?> list) {
		StringBuilder buffer = new StringBuilder();
		for(Object obj : list) {
			buffer.append(String.valueOf(obj)).append("|");
		}
		return buffer.toString();
	}
}
